#include "OffsetReg.h"
#include "p24HJ256GP206.h"
#include "defines.h"
#include "IOpin.h"

unsigned int GainReg=0;
void OffsetCompensation(unsigned int gain, unsigned int Zero)
{
float compf=0;
PinOff(COIL_1_PIN,COIL_1_PORT);
PinOff(COIL_2_PIN,COIL_2_PORT);
if(Zero<2048-100 || Zero>2048+100)
      {
  
      /*if(Zero<1048 || Zero>3048)
        OffsetRegPause=500;
      else
         OffsetRegPause=500;*/

           
       
         switch (gain)
         {
         
        case 470:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.04);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
         case 940:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.02);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
         case 1880:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.015);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
         case 2350:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.01);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
         case 3760:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.005);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
          case 4700:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.003);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
          case 7520:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.0015);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
            
         case 15040:
            
            compf=(float)OC8RS+(((float)Zero-(float)2048)*0.0008);
            OC8RS=(unsigned int)compf;
            goto OverAdj;
            break;
            
       default:
            break;
         }
            
        
         OverAdj:
         
         if(OC8RS>40000)
            OC8RS=39999;
         
    
      
      }
     return;    
   }
